package com.wd.live.view;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.wd.live.R;
import com.wd.live.base.BaseActivity;
import com.wd.live.base.BaseViewModel;
import com.wd.live.bean.MusicBeen;
import com.wd.live.databinding.ActivityMusicBinding;
import com.wd.live.viewmodel.MusicViewModel;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class MusicActivity extends BaseActivity<MusicViewModel, ActivityMusicBinding> {

    public static MediaPlayer mp;
    boolean a;


    @Override
    protected void initData() {
        mp=new MediaPlayer();
        viewmodel.getMusicData(1,10).observe(this, new Observer<MusicBeen>() {
            @Override
            public void onChanged(MusicBeen musicBeen) {
                List<MusicBeen.ResultBean> result = musicBeen.getResult();
                databind.musicrec.setLayoutManager(new LinearLayoutManager(MusicActivity.this));
                databind.musicrec.setAdapter(new CommonAdapter<MusicBeen.ResultBean>(MusicActivity.this,R.layout.music_item,result) {

                    @Override
                    protected void convert(ViewHolder holder, MusicBeen.ResultBean resultBean, int position) {
                        Glide.with(MusicActivity.this).load(resultBean.getCover()).into((ImageView) holder.getView(R.id.music_img));
                        holder.setText(R.id.music_name,resultBean.getSinger());
                        holder.setText(R.id.music_title,resultBean.getName());

                        try {
                            mp.reset();
                            mp.setDataSource(resultBean.getMusicLinks());
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        holder.setText(R.id.start_time,resultBean.getDuration());
                        ImageView zt = holder.getView(R.id.zt);
                        zt.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                try {
                                    mp.reset();
                                    mp.setDataSource(resultBean.getMusicLinks());
                                    Log.v("links",resultBean.getMusicLinks());
                                    mp.prepare();
                                    mp.start();

                                } catch (IOException e) {
                                    e.printStackTrace();
                                }

                                if(mp.isPlaying()){
                                    if(a){
                                        mp.pause();
                                        zt.setImageResource(R.mipmap.play1);
                                    }else{
                                        mp.start();
                                        zt.setImageResource(R.mipmap.play);
                                    }
                                }

                                a=!a;
                            }
                        });

                    }
                });
            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(mp!=null){
            mp.stop();
            mp.release();

        }
    }

    @Override
    protected int initLayout() {
        return R.layout.activity_music;
    }

    @Override
    public void onChanged(Object o) {

    }
}